<template>
  <div id="windowDiffShowComponent" v-loading="loading_file_show">
    <code-diff
        :old-string="old_value"
        :new-string="new_value"
        :drawFileList="true"
        :file-name="'对比 【' + old_path_url + '】 和 【' + new_path_url + '】'"
        output-format="side-by-side"/>
  </div>

</template>

<script>
import axios from 'axios'
import {CodeDiff} from 'v-code-diff'

export default {
  name: "windowDiffShowComponent",
  props: {
    sessionToken: String,
    editFileUrl: String,
    width: Number,
    height: Number,
    old_path_url: String,
    new_path_url: String,
    curr_editableTabsValue: String,
  },
  components: {
    CodeDiff
  },
  data() {
    return {
      // old_path_url: "\\source_gitee\\openEuler-20.03-LTS-Next\\rpm\\rpm.spec",
      // new_path_url: "\\source_gitee\\openEuler-20.03-LTS-SP2\\rpm\\rpm.spec",
      old_value: "/* 历史文件正在加载中... */\n",
      new_value: "/* 当前文件正在加载中... */\n",
      loading_file_show: false,
      // edit_file_str: "",
    }
  },
  methods: {
    load_file_with_session_token(url, call_bak) {
      if (this.spec_path === "") return
      let obj_this = this
      obj_this.loading_file_show = true
      let data = {
        "session_token": obj_this.sessionToken,
        "file_path": url
      }
      axios.post('/get_file_str_with_session_token', data).then(function (response) {
        call_bak(response.data.data)
        obj_this.loading_file_show = false
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      }).then(function () {

      });
    },
    save(type) {
      // console.log(this.curr_editableTabsValue, this.$store.state.curr_editor_file_path, type)
      console.log(this.editFileUrl + `content saved by ${type}`)
    },
    saveContent(e) {
      let key = window.event.keyCode ? window.event.keyCode : window.event.which
      if (key === 83 && e.ctrlKey) {
        if (this.curr_editableTabsValue === this.$store.state.curr_editor_file_path) {
          this.save('hot key')
        }
        e.preventDefault()
      }
    }
  },
  mounted() {
    let obj_this = this
    this.load_file_with_session_token(this.old_path_url, function (value) {
      obj_this.old_value = value
    })
    this.load_file_with_session_token(this.new_path_url, function (value) {
      obj_this.new_value = value
    })
    document.addEventListener('keydown', this.saveContent)
  }
}
</script>

<style scoped>
</style>